/*************************************************************************
	> File Name: qsorter.c
	> Author: Angel Criss
	> Mail: abbytsing@gmail.com 
	> Created Time: 六  8/ 9 08:29:15 2014
 ************************************************************************/
// quick sort by using qsort()

#include<stdio.h>
#include<stdlib.h>
#define NUM 50

void makearray(double arr[],int n)
{
    int index;

    for(index=0;index<n;index++)
        arr[index]=(double)rand() / ((double)rand() + 0.1); // use rand() to generate random number
}

void showarray(const double arr[],int n)
{
    int index;

    for(index=0;index<n;index++)
    {
        printf("%9.4f ",arr[index]);
        if(index % 6 == 5)
            putchar('\n');
    }
    if(index % 6 != 0)
        putchar('\n');
}

int comp(const void *p1,const void *p2)
{
    const double *a1=(const double *)p1;
    const double *a2=(const double *)p2;

    if(*a1<*a2)
        return -1;
    else if (*a1 == *a2)
        return 0;
    else
        return 1;
}

int main(void)
{
    double value[NUM];
    makearray(value,NUM);
    puts("Random NUmber List:\n");
    showarray(value,NUM);
    
    qsort(value,NUM,sizeof(double),comp);
    
    putchar('\n');
    showarray(value,NUM);
    return 0;
}
